package de.mischa.nichtraucher;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.ScrollView;
import android.widget.TimePicker;

public class NichtraucherActivity extends Activity {

	
	private DbAdapter dbAdapter;
	private TimePicker timePicker;
	private DatePicker datePicker;
	private EditText txtCount;
	private EditText txtParcelCount;
	private EditText txtParcelCost;
	private EditText txtCurrency;
	private Cursor cursor;
	private String currency;
	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private int count = 0;
	private double parcelCost = 0;
	private int parcelCount = 0;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ScrollView sv = new ScrollView(this);
		sv.setScrollContainer(true);
		setTitle(R.string.app_name);
		
		dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		txtCount = (EditText) findViewById(R.id.txtHowMany);
		txtParcelCost = (EditText) findViewById(R.id.txtParcelCost);
		txtParcelCount = (EditText) findViewById(R.id.txtParcelCount);
		txtCurrency = (EditText)findViewById(R.id.txtCurrency);
		datePicker = (DatePicker) findViewById(R.id.datePicker);
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		timePicker.setIs24HourView(true);

		cursor = dbAdapter.fetchEntry();
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			datePicker.init(
					cursor.getInt(cursor.getColumnIndex(DbAdapter.KEY_YEAR)),
					cursor.getInt(cursor.getColumnIndex(DbAdapter.KEY_MONTH)),
					cursor.getInt(cursor.getColumnIndex(DbAdapter.KEY_DAY)),
					null);
			timePicker.setCurrentHour(cursor.getInt(cursor
					.getColumnIndex(DbAdapter.KEY_HOUR)));
			timePicker.setCurrentMinute(cursor.getInt(cursor
					.getColumnIndex(DbAdapter.KEY_MINUTE)));
			txtCount.setText(cursor.getString(cursor
					.getColumnIndex(DbAdapter.KEY_COUNT)));
			txtParcelCost.setText(cursor.getString(cursor
					.getColumnIndex(DbAdapter.KEY_PARCELCOST)));
			txtParcelCount.setText(cursor.getString(cursor
					.getColumnIndex(DbAdapter.KEY_PARCELCOUNT)));
			txtCurrency.setText(cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_CURRENCY)));
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		dbAdapter = new DbAdapter(this);
		dbAdapter.open();
	}

	@Override
	protected void onPause() {
		saveChanges();
		cursor.close();
		dbAdapter.close();
		super.onPause();
	}

	private void updateWidget() {
		if (count > 0 && parcelCount > 0) {

			RemoteViews view = new RemoteViews(this.getPackageName(),
					R.layout.widget);

			Resources res = this.getResources();
			view.setTextViewText(R.id.lblNotSmokedW,
					res.getString(R.string.notSmoked));
			view.setTextViewText(R.id.lblSavedMoneyW,
					res.getString(R.string.moneySaved));
			view.setTextViewText(R.id.lblSinceW, res.getString(R.string.since));
			
			
			Calendar cal_1 = new GregorianCalendar();
			Calendar cal_2 = new GregorianCalendar();
			cal_1.set(year, month, day, hour, minute);

			long time = cal_2.getTimeInMillis() - cal_1.getTimeInMillis(); // Differenz
																			// in
																			// ms

			long allMinutes = time / (1000 * 60);

			long days = time / (1000 * 60 * 60 * 24);
			time = time - (days * 24 * 60 * 60 * 1000);
			long hours = time / (1000 * 60 * 60);
			time = time - (hours * 60 * 60 * 1000);
			long minutes = time / (1000 * 60);
			double oneCigaretteTime = (24 * 60) / count;
			int notSmoked = new Double(allMinutes / oneCigaretteTime)
					.intValue();
			double oneCigaretteCost = parcelCost / parcelCount;
			double moneySaved = notSmoked * oneCigaretteCost;
			DecimalFormat decimalFormat;
			decimalFormat = new DecimalFormat("#,##0.00");

			view.setTextViewText(R.id.txtDaysW, days + " "+getResources().getString(R.string.days));
			view.setTextViewText(R.id.txtHoursW, hours + " "+getResources().getString(R.string.hours));
			view.setTextViewText(R.id.txtMinutesW, minutes + " "+getResources().getString(R.string.minutes));
			view.setTextViewText(R.id.txtNotSmokedCountW,
					String.valueOf(notSmoked));
			view.setTextViewText(R.id.txtMoneySavedW,
					decimalFormat.format(moneySaved) + currency);

			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(this);
			appWidgetManager.updateAppWidget(new ComponentName(this,
					Widget.class), view);

			finish();
		}
	}

	private void saveChanges() {
		year = datePicker.getYear();
		month = datePicker.getMonth();
		day = datePicker.getDayOfMonth();
		hour = timePicker.getCurrentHour();
		minute = timePicker.getCurrentMinute();
		currency = txtCurrency.getText().toString();
		if(currency.length()>3){
			currency = currency.substring(0, 2);
		}
		count = 0;
		parcelCost = 0;
		parcelCount = 0;
		try {
			count = Integer.parseInt(txtCount.getText().toString());
			parcelCost = Double.parseDouble(txtParcelCost.getText().toString());
			parcelCount = Integer.parseInt(txtParcelCount.getText().toString());
		} catch (NumberFormatException e) {

		}
		Cursor cursor = dbAdapter.fetchEntry();
		if (cursor.getCount() > 0) {
			dbAdapter.updateEntry(year, month, day, hour, minute, count,
					parcelCost, parcelCount,currency);
		} else {
			dbAdapter.createEntry(year, month, day, hour, minute, count,
					parcelCost, parcelCount,currency);
		}

		updateWidget();
	}
}